import { connectorIdDic, SvrType } from "../../../config/SvrCfg";
import { gateMain } from "../GateMain";
import { IFSess } from "../../../common/interfaces";
import { MsgHandler } from "kilom";
import { MsgId, MsgTos, MsgVo } from "../../../config/Msg";

export default class MsgGate extends MsgHandler {

    /**
     * 获取服务器列表
     */
    getSvrList(sess: IFSess) {
        let svrList: MsgVo.login.Svr[] = [];
        let games = this.app.svrs[SvrType.game];
        for (let game of games) {
            let onlines = gateMain.getSidOnlines(game.args.sid);
            svrList.push({
                sid: game.args.sid,
                name: game.args.name,
                state: 1,
                load: Math.min(3, onlines)
            });
        }

        sess.send(MsgId.getSvrList, { svrList });
    }

    /**
     * 获取服务器地址
     */
    getSvrHost(sess: IFSess, msg: MsgTos[MsgId.getSvrHost]) {
        let svr = this.app.svrCfgIdMap[connectorIdDic[msg.sid]];
        sess.send(MsgId.getSvrHost, { sid: msg.sid, host: svr ? `ws://${svr.host}:${svr.clientPort}` : null });
    }
}